package proxy.statics.order;

import proxy.modules.DynamicDataSourceEntry;
import proxy.modules.IOrderService;
import proxy.modules.Order;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Feng
 * @date 2019/3/12 20:56
 */
public class OrderServiceStaticProxy implements IOrderService {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");

    private IOrderService orderService;

    public OrderServiceStaticProxy(IOrderService orderService){
        this.orderService = orderService;
    }

    @Override
    public int creatOrder(Order order) {
        before();
        Long time = order.getCreatTime();
        int dbRouter = Integer.valueOf(simpleDateFormat.format(new Date(time)));
        System.out.println("静态代理类自动分配到【DB_ "+ dbRouter +"】数据源处理数据");
        DynamicDataSourceEntry.set(dbRouter);
        orderService.creatOrder(order);
        after();
        DynamicDataSourceEntry.restore();
        return 0;
    }

    private void after() {
        System.out.println("Proxy after");
    }

    private void before() {
        System.out.println("Proxy before");
    }
}
